<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script>
    //异步相关例子
    async function doubleAfter2seconds(num) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          // throw new Error("测试错误执行");    //会直接在此处报错
          resolve(2 * num)
        }, 1000);
      })
    }

    //只要带上async执行完成的结果会打包成Promise返回的结果 没有报错执行结果.then 有报错执行结果.catch
    async function doubleAfter2seconds2(num) {
      // setTimeout(() => {
      //   throw new Error("测试错误执行");    //会直接在此处报错  其实用了qsync会有异常处理这里用了setTimeOut导致无效才会在此处报错
      //   resolve(2 * num)
      // }, 1000);

      var obj =  { code: -1, msg: '测试错误执行' }
      throw new Error(JSON.stringify(obj));    //不会直接在此处报错
      throw new Error("测试错误执行");    //会直接在此处报错
      return (2 * num);     //带async直接返回常规数据会自动包装成Promise
    }

    async function testResult() {
      // let result = await doubleAfter2seconds(30);   //await等于使用结果.then()方法取成功运行的数据
      // console.log(result);

      // let result = doubleAfter2seconds(30);
      // console.log(result);
      // result.then((resp)=>{
      //   console.log("then输出：",resp);
      // }).catch((err)=>{
      //   console.log("catch输出：",err);
      // });

      // let result = doubleAfter2seconds2(30);   //await等于使用结果.then()方法取成功运行的数据
      // console.log(result);
      let result = doubleAfter2seconds2(30);
      // console.log(result);
      result.then((resp) => {
        console.log("then输出：", resp);
      }).catch((err) => {
        console.log("catch输出：", err);
      });


    }

    testResult();

  </script>

</head>

<body>

</body>

</html>